<script setup>
import { ref } from 'vue'
import { useEmployeeStore } from '@/stores'
import { employeeUpdateMeService } from '@/api/employee'
// 表单数据
const formModel = ref()

const employeeStore = useEmployeeStore()

// 初始化表单数据
const employeeInfo = employeeStore.employeeInfo
// 注意要解构，直接赋值就会双向绑定
formModel.value = { ...employeeInfo }

const formRules = {
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { pattern: /^[^\d]*$/, message: '名字格式错误', trigger: 'blur' }
  ],
  age: [
    { required: true, message: '请输入年龄', trigger: 'blur' },
    { pattern: /^\d{1,3}$/, message: '年龄格式错误', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误', trigger: 'blur' }
  ],
  sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }]
}

// 表单对象
const form = ref()

// 重置表单内容
const resetForm = async () => {
  await ElMessageBox.confirm('你确认重置表单数据吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  formModel.value = { ...employeeInfo }
}

// 提交表单修改
const submitForm = async () => {
  await form.value.validate()
  await employeeUpdateMeService(formModel.value).then((response) => {
    employeeStore.setEmployeeInfo(formModel.value)
    ElMessage.success(response.message)
  })
}
</script>

<template>
  <el-card>
    <template #header>
      <h1>个人信息</h1>
    </template>

    <el-container>
      <el-main>
        <el-form
          :model="formModel"
          :rules="formRules"
          label-width="100px"
          size="large"
          ref="form"
          :hide-required-asterisk="true"
        >
          <el-form-item label="姓名" prop="name">
            <el-input
              v-model="formModel.name"
              placeholder="请输入姓名"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="性别" prop="sex">
            <el-select
              v-model="formModel.sex"
              placeholder="请选择性别"
              clearable
            >
              <el-option label="男" value="男" />
              <el-option label="女" value="女" />
            </el-select>
          </el-form-item>

          <el-form-item label="年龄" prop="age">
            <el-input
              v-model="formModel.age"
              placeholder="请输入年龄"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="手机号" prop="phone">
            <el-input
              v-model="formModel.phone"
              placeholder="请输入手机号"
              clearable
            />
          </el-form-item>

          <el-form-item style="padding-left: 50px; margin-top: 20px">
            <el-button type="info" @click="resetForm">重置</el-button>
            <el-button type="primary" @click="submitForm">提交</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </el-card>
</template>

<style lang="scss" scoped>
.el-card {
  height: 100%;

  .el-main {
    padding: 0;

    .el-form {
      width: 30%;
    }
  }
}
</style>
